package com.csproj.basic.repository.impl;

import com.csproj.basic.domain.Area;
import com.csproj.basic.repository.custom.AreaRepositoryCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;

public class AreaRepositoryImpl implements AreaRepositoryCustom {
    @PersistenceContext
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    /**
     * 机构分页查询方法
     * @param map	参数
     * @param pageable		分页对象
     * @return
     */
    public Page<Area> findPageList(Map<String, Object> map, Pageable pageable) {
        //拼hql
        String hql = " from Area wh where wh.deletedFlag = 0 ";
        Map<String,Object> queryParams = new HashMap<>();

        String siteId= (String)map.get("siteId");
        String warehouseId = (String)map.get("warehouseId");
        String areaId = (String)map.get("areaId");
        if(StringUtils.isNotBlank(siteId)&&!siteId.equals("nositeID")){
            hql += " and wh.siteId = :siteId ";
            queryParams.put("siteId",siteId);
        }
        if(StringUtils.isNotBlank(warehouseId)){
            hql += "  and wh.warehouseId = :warehouseId ";
            queryParams.put("warehouseId",warehouseId);
        }
        if(StringUtils.isNotBlank(areaId)){
            hql += "  and wh.id = :areaId ";
            queryParams.put("areaId",areaId);
        }
        //查询
        Page<Area> fieldList = query(hql, pageable, queryParams);
        //返回
        return fieldList;
    }
}
